package com.cgw.game.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.cgw.game.R;
import com.cgw.game.utils.MyBarLineChartTouchListener;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BarChartMoreActivity extends AppCompatActivity {
    BarChart barChart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar_chart_more);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getIntent().getStringExtra("title"));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//返回
            }
        });
        barChart = findViewById(R.id.bar_chart);
        initBar();
        setData();
        barChart.setOnTouchListener(new MyBarLineChartTouchListener(barChart,barChart.getViewPortHandler().getMatrixTouch(),3f));
    }
    private void initBar() {
        barChart.setExtraBottomOffset(5f);
        barChart.setTouchEnabled(true);
        barChart.setPinchZoom(false);
        barChart.setScaleYEnabled(false);
        barChart.setDoubleTapToZoomEnabled(false);
        barChart.animateY(1000);
        //取消说明
        barChart.getLegend().setEnabled(false);
        barChart.getDescription().setEnabled(false);
        barChart.setNoDataTextColor(getResources().getColor(R.color.colorAccent));
        barChart.setNoDataText("暂无数据");


        //竖向轴线设置
        YAxis leftAxis = barChart.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.setTextSize(12f);
        leftAxis.setTextColor(Color.parseColor("#91a2a3"));
        leftAxis.setEnabled(true);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setDrawAxisLine(false);
        leftAxis.setGridColor(Color.parseColor("#e3edf2"));
        leftAxis.setDrawAxisLine(false);
        barChart.getAxisRight().setEnabled(false);

        //横向轴线设置
        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(12);
        xAxis.setTextColor(Color.parseColor("#91a2a3"));
        xAxis.setEnabled(true);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setAxisLineColor(Color.parseColor("#e3edf2"));
        //设置为虚线
        xAxis.setDrawAxisLine(true);

    }

    private void setData() {
        List<BarEntry> barValue = new ArrayList<>();
        for (int i = 100; i < 200; i++) {
            barValue.add(new BarEntry(i, new Random().nextInt(100)+1));
        }
        BarDataSet barDataSet;
        barDataSet = new BarDataSet(barValue, "LAR");
        barDataSet.setColor(getResources().getColor(R.color.colorAccent)); // 设置柱形图颜色
        barDataSet.setHighLightColor(getResources().getColor(R.color.colorPrimaryDark)); // 设置柱形高亮颜色
        barDataSet.setDrawValues(false);
        BarData barData = new BarData();
        barData.addDataSet(barDataSet);// 添加一组柱形图，如果有多组柱形图数据，则可以多次addDataSet来设置
        barData.setBarWidth(0.8f);
        barChart.setData(barData);
        barChart.setVisibleXRangeMaximum(8);
        barChart.moveViewToX(150);
    }
}